package com.xtaller.lazy.lib.kit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019/3/26
 * @Description: 时间处理类
 */
public class TimeUtil {
    /**
     * 获取当前服务器时间Date
     * */
    private static Date now(){
        return new Date();
    }

    /**
     * 将时间转换为时间戳
     */
    public static long dateToStamp(String s) {
        long res = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
            res = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return res > 0 ? res/1000 : 0;
    }
    /**
     * 获取当前时间戳
     * */
    public static Integer getTimestamp(){
        return Integer.parseInt((System.currentTimeMillis()/1000)+"");
    }

    public static long getTimestamps(){
        return System.currentTimeMillis();
    }
    /**
     * 格式化当前时间
     * @param format
     *      时间格式 例如：yyyy-MM-dd
     * */
    public static String formatNow(String format){
        return new SimpleDateFormat(format)
                .format(now());
    }
    /**
     * 格式化当前时间
     * @param date
     *      时间
     * @param format
     *      时间格式 例如：yyyy-MM-dd
     * */
    public static String formatDate(Date date,String format){
        return new SimpleDateFormat(format)
                .format(date);
    }
    public static String formatDate(String format){
        return new SimpleDateFormat(format)
                .format(new Date().getTime());
    }
    /**
     * 获得所在星期的第一天
     */
    public static Date getFirstDateByWeek(Date date) {

        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int today = now.get(Calendar.DAY_OF_WEEK);
        int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
        now.set(now.DATE, first_day_of_week);
        return now.getTime();
    }

    /**
     * 获得所在星期的最后一天
     */
    public static Date getLastDateByWeek() {
        Calendar now = Calendar.getInstance();
        now.setTime(now());
        int today = now.get(Calendar.DAY_OF_WEEK);
        int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
        int last_day_of_week = first_day_of_week + 6; // 星期日
        now.set(now.DATE, last_day_of_week);
        return now.getTime();
    }

    /**
     * 获得所在月份的最后一天
     */
    public static Date getLastDateByMonth() {
        Calendar now = Calendar.getInstance();
        now.setTime(now());
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1);
        now.set(Calendar.DATE, 1);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - 1);
        now.set(Calendar.HOUR, 11);
        now.set(Calendar.MINUTE, 59);
        now.set(Calendar.SECOND, 59);
        return now.getTime();
    }

    /**
     * 获得所在月份的第一天
     */
    public static Date getFirstDateByMonth() {
        Calendar now = Calendar.getInstance();
        now.setTime(now());
        now.set(Calendar.DATE, 0);
        now.set(Calendar.HOUR, 12);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        return now.getTime();
    }

    /**
     * 检查一个字符串是否是日期格式
     * */
    public static boolean checkDate(String date){
        boolean check = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try{
            Date d = sdf.parse(date);
            check = true;
        }catch (Exception ex){

        }
        return check;
    }

    /**
     * 将一个字符串转成对应的时间格式
     * */
    public static Date s2d(String date, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 计算2个日期之间的天数
     * */
    public static int dayBetween(String bDate, String eDate){
        Date d1 = s2d(bDate, "yyyy-MM-dd");
        Date d2 = s2d(eDate, "yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(d1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(d2);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

}
